package cards.controller;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.code.kaptcha.Producer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import org.springframework.ui.Model;
import cards.service.RegisterService;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;


/**
 * 注册接口
 *
 * @author xiezhiping
 *
 */
@Controller
public class RegisterController {

	@Autowired
	RegisterService registerService;

	@Autowired
	private Producer kaptchaProducer;
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

	@RequestMapping(path = "/kaptcha", method = RequestMethod.GET)
	public void getKaptcha(HttpServletResponse response, HttpSession session){
		// 生成验证码
		String text = kaptchaProducer.createText();
		BufferedImage image = kaptchaProducer.createImage(text);
		// text存入session
		session.setAttribute("kaptcha", text);
		// image输出给浏览器
		response.setContentType("image/png");
		System.out.println("响应验证码之前：");
		try {
			OutputStream os = response.getOutputStream();
			ImageIO.write(image, "png", os);
		} catch (IOException e) {
			logger.error("响应验证码失败：" + e.getMessage());
			System.out.println("响应验证码失败：" + e.getMessage());
		}
	}

	@RequestMapping(path = "/register", method = RequestMethod.GET)
	public String register() {
		return "register";
	}

	@RequestMapping(path = "/register", method = RequestMethod.POST)
	public View register(@RequestParam("username") String username, @RequestParam("password") String password,
						 @RequestParam("introduction") String introduction, @RequestParam("nickname") String nickname,
						 @RequestParam("gender") int gender, @RequestParam("phoneNumber") String phoneNumber,
						 @RequestParam("kaptcha") String kaptcha,Model model,
						 HttpServletRequest request, HttpSession session) {
		model.addAttribute("username", username);
		model.addAttribute("password", password);
		model.addAttribute("introduction", introduction);
		model.addAttribute("nickname", nickname);
		model.addAttribute("gender", gender);
		model.addAttribute("phoneNumber", phoneNumber);
		//尝试保留已经填写的信息
		String contextPath = request.getContextPath();
		//检查验证码
		String kaptchaValue = (String) session.getAttribute("kaptcha");
		model.addAttribute("gender", 0);
		if(StringUtils.isBlank(kaptchaValue) || StringUtils.isBlank(kaptcha) || !kaptchaValue.equalsIgnoreCase(kaptcha)){
			model.addAttribute("codeMsg", "验证码不正确");
			return new RedirectView(contextPath + "/register");
		}

		int permission = 0;//一律设为user，管理员只能后台设置。
		String action = registerService.reg(username, password, introduction, nickname, gender, phoneNumber, permission);
		return new RedirectView(contextPath + action);
	}
}

